package elevatronic.view.simulation;

import java.util.ArrayList;
import java.util.List;

import elevatronic.models.Building;
import elevatronic.models.Elevator;
import elevatronic.models.Floor;
import elevatronic.models.People;
import elevatronic.view.ImageView;

public class BuildingView{
	
	//the model of the view
	private Building building;
	public Building getBuilding() {
		return building;
	}
	public void setBuilding(Building building) {
		this.building = building;
	}
	
	//singleton, indeed, there is only one Building instance and so only one BuildingView instance in the application, so, why don't access to it easily ? ^^
	private static BuildingView singleton;
	public static BuildingView getSingleton() {
		return singleton;
	}
	public static void setSingleton(BuildingView _singleton) {
		singleton = _singleton;
	}
	
	//contain all elevators
	private List<ElevatorView> elevatorsView;
	public List<ElevatorView> getElevatorsView() {
		return elevatorsView;
	}
	public void setElevatorsView(List<ElevatorView> elevatorsView) {
		this.elevatorsView = elevatorsView;
	}
	
	//contain all floors
	private List<FloorView> floorsView;
	public List<FloorView> getFloorsView() {
		return floorsView;
	}
	public void setFloorsView(List<FloorView> floorsView) {
		this.floorsView = floorsView;
	}
	
	//contain all people
	public List<PeopleView> getPeopleView() {
		List<PeopleView> retour=new ArrayList<PeopleView>();
		for(FloorView floorView:getFloorsView()){
			retour.addAll(floorView.getPeopleView());
		}
		for(ElevatorView elevatorView:getElevatorsView()){
			retour.addAll(elevatorView.getPeopleView());
		}		
		return retour;
	}
	
	//contain all images of the building
	private List<ImageView> images;	
	public List<ImageView> getImages() {
		return images;
	}
	public void setImages(List<ImageView> images) {
		this.images = images;
	}
	
	//image of the building
	private ImageView imageBuilding;
	public ImageView getImageBuilding() {
		return imageBuilding;
	}
	public void setImageBuilding(ImageView imageBuilding) {
		this.imageBuilding = imageBuilding;
	}
	
	//constructor
	public BuildingView(Building building){
		setSingleton(this);
		setBuilding(building);
		setImageBuilding(new ImageView("building",0,0,800,800));		
		
		//add elevator and floor views
		setElevatorsView(new ArrayList<ElevatorView>());
		for(Elevator elevator:getBuilding().getElevators()){
			getElevatorsView().add(new ElevatorView(elevator));
		}

		setFloorsView(new ArrayList<FloorView>());
		for(Floor floor:getBuilding().getFloors()){
			getFloorsView().add(new FloorView(floor));
		}		
	}
	
	
	
	//iteration of the building
	public void update(int step){
		for(PeopleView peopleView:getPeopleView()){
			peopleView.update(step);
		}
	}
	
	
	//this method gather all people on the scene in one list
	public List<People> getPeople(){
		List<People> retour=new ArrayList<People>();
		for(Elevator elevator:getBuilding().getElevators()){
			retour.addAll(elevator.getPeopleInElevator());
		}
		for(Floor floor:getBuilding().getFloors()){
			retour.addAll(floor.getPeopleAtFloor());
		}		
		return retour;
	}
}
